package net.knightwatch.kwtools.customers;

import java.util.ArrayList;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

public class AddressAutoCompleteAdapter extends ArrayAdapter<Address> implements Filterable {
    private ArrayList<Address> mData;
	public static final String TAG = "AddressArrayAdapter";
	int resource;
	String response;
	Context context;
	/**
	 * Default constructor for BuildingArrayAdapter
	 * @param context
	 * @param resource
	 * @param buildings
	 */
	public AddressAutoCompleteAdapter(Context context, int textViewResourceId, ArrayList<Address> addresses) {
        super(context, textViewResourceId);
        mData = new ArrayList<Address>();
        mData = addresses;
	}
	@Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Address getItem(int index) {
        return mData.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if(constraint != null) { 
                    filterResults.values = mData;
                    filterResults.count = mData.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                if(results != null && results.count > 0) {
                notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return myFilter;
    }
}
